<template>
  <div>
    <BasicTable
      @register="registerTable"
      :rowSelection="{ type: 'checkbox', selectedRowKeys: checkedKeys }"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '编辑',
                tooltip: '编辑',
                onClick: handleAdd.bind(null, 2, record),
              },
              {
                label: '列表',
                tooltip: '列表',
                onClick: handleList.bind(null, record),
              },
              {
                label: '删除',
                tooltip: '删除',
                onClick: handleDel.bind(null, record),
              },
            ]"
        /></template>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleAdd(1, null)">新建</a-button>
      </template>
      <template #status="{ column, record }">
        <Badge
          v-if="column.dataIndex === 'status'"
          :status="statusMap[record.status].status"
          :text="statusMap[record.status].text"
        />
      </template>
    </BasicTable>
    <AddedModal @register="register" @reload="reload" />
    <ListModal @register="registerList" />
  </div>
</template>

<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import AddedModal from './modal/ added.vue';
  import ListModal from './modal/list/index.vue';
  import { Badge, Modal } from 'ant-design-vue';
  import { DictCategoryList, List, TypeRemove } from '@/api/platform/dictionary';
  import { getBasicColumns } from './tableData';
  import Dictionary from '@/utils/dictionary';
  import { ref } from 'vue';
  import { useModal } from '@/components/Modal';
  import { ITypeListResult } from '@/api/platform/dictionary/type';
  import { useMessage } from '@/hooks/web/useMessage';

  const checkedKeys = ref<(string | number)[]>([]);

  const dicts = Dictionary.getLocalMulti(['dictStatus']);
  const { createMessage } = useMessage();

  const statusMap = ref({
    0: {
      status: 'success',
      text: '正常',
    },
    1: {
      status: 'default',
      text: '禁用',
    },
  });

  const [registerTable, { reload }] = useTable({
    title: '',
    api: List,
    columns: getBasicColumns(),
    rowKey: 'dictId',
    formConfig: {
      labelWidth: 100,
      schemas: [
        {
          field: `dictName`,
          label: `字典名称`,
          component: 'Input',
          colProps: {
            span: 5,
          },
        },
        {
          field: `dictType`,
          label: `字典类型`,
          component: 'Input',
          colProps: {
            span: 5,
          },
        },
        {
          field: `dictCategoryId`,
          label: `字典分类`,
          component: 'ApiSelect',
          colProps: {
            span: 5,
          },
          componentProps: {
            api: DictCategoryList,
            labelField: 'name',
            valueField: 'id',
          },
        },
        {
          field: `status`,
          label: `状态`,
          component: 'Select',
          componentProps: {
            fieldNames: { label: 'dictLabel', value: 'dictValue' },
            options: dicts.dictStatus,
          },
          colProps: {
            span: 4,
          },
        },
      ],
    },
    useSearchForm: true,
    showTableSetting: true,
    tableSetting: { fullScreen: true },
    showIndexColumn: true,
    clickToRowSelect: false,
    actionColumn: {
      width: 100,
      title: '操作',
      dataIndex: 'action',
    },
  });

  const [register, { openModal }] = useModal();
  const [registerList, { openModal: openListModal }] = useModal();

  function handleAdd(type: number, row: any) {
    openModal(true, {
      type,
      row,
    });
  }

  function handleList(row: ITypeListResult) {
    openListModal(true, {
      dictType: row.dictType,
    });
  }

  function handleDel(row: ITypeListResult) {
    Modal.confirm({
      title: '提示',
      content: '该操作将删除该字典',
      onOk: async () => {
        await TypeRemove(row.dictId);
        createMessage.success('删除成功！');
        reload();
      },
    });
  }
</script>
